package com.application.chiettinhdienluc.ui;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.application.chiettinhdienluc.R;
import com.application.chiettinhdienluc.app.MainApplication;
import com.application.chiettinhdienluc.data.GlobalData;
import com.application.chiettinhdienluc.utils.CalculationUtils;
import com.application.chiettinhdienluc.utils.Utils;

public class ResultInfoFragment extends Fragment implements OnClickListener {
	private final String TAG = ResultInfoFragment.class.getSimpleName();
	private final int FROM_TV_CHIPHI_VC = 0;
	private final int FROM_TV_CHIPHI_MAY_THI_CONG = 1;
	
	private Resources res;
	private TextView tv_userinfo;
	private EditText edt_cost_vatlieu;
	private EditText edt_cost_vatlieu1;
	private TextView edt_vanchuyen_cost;
	private ImageButton imgbtn_edit_vanchuyen_cost;
	private EditText edt_nhancong_cost;
	private TextView edt_maythicong_cost;
	private ImageButton imgbtn_edit_maythicong_cost;
	private EditText edt_tructiep_khac_cost;
	private EditText edt_tructiep_cost;
	private EditText edt_general_cost;
	private EditText edt_tl_cost;
	private EditText edt_k_cost;
	private EditText edt_gtt_cost;
	private EditText edt_vat_cost;
	private EditText edt_total_cost;
	
	LinearLayout ttk, tl, k ,gtt, vat ;
	
//	private TextView tv_maythicong_cost, tv_vanchuyen_cost;
	
	
	public static boolean needRefreshEmptyValueResultInfo = false;

	private boolean UpdateListenerIsRegistered = false;
	private UpdateListener listener = null;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getActivity().getWindow().setBackgroundDrawable(null);
	}

	/**
	 * The Fragment's UI
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.result_info, container, false);

		edt_cost_vatlieu = (EditText)v.findViewById(R.id.edt_cost_vatlieu);
		edt_cost_vatlieu1 = (EditText)v.findViewById(R.id.edt_cost_vatlieu1);
		edt_vanchuyen_cost = (TextView)v.findViewById(R.id.edt_vanchuyen_cost);
		edt_nhancong_cost = (EditText)v.findViewById(R.id.edt_nhancong_cost);
		edt_maythicong_cost = (TextView)v.findViewById(R.id.edt_maythicong_cost);
		edt_tructiep_khac_cost = (EditText)v.findViewById(R.id.edt_tructiep_khac_cost);
		edt_tructiep_cost = (EditText)v.findViewById(R.id.edt_tructiep_cost);
		edt_general_cost = (EditText)v.findViewById(R.id.edt_general_cost);
		edt_tl_cost = (EditText)v.findViewById(R.id.edt_tl_cost);
		edt_k_cost = (EditText)v.findViewById(R.id.edt_k_cost);
		edt_gtt_cost = (EditText)v.findViewById(R.id.edt_gtt_cost);
		edt_vat_cost = (EditText)v.findViewById(R.id.edt_vat_cost);
		edt_total_cost = (EditText)v.findViewById(R.id.edt_total_cost);
		imgbtn_edit_vanchuyen_cost = (ImageButton)v.findViewById(R.id.imgbtn_edit_vanchuyen_cost);
		imgbtn_edit_maythicong_cost = (ImageButton)v.findViewById(R.id.imgbtn_edit_maythicong_cost);
		
		ttk = (LinearLayout)v.findViewById(R.id.ttk_layout);
		tl = (LinearLayout)v.findViewById(R.id.tl_layout);
		k = (LinearLayout)v.findViewById(R.id.k_layout);
		gtt = (LinearLayout)v.findViewById(R.id.gtt_layout);
		vat = (LinearLayout)v.findViewById(R.id.vat_layout);
		
//		tv_maythicong_cost = (TextView)v.findViewById(R.id.tv_maythicong_cost);		
//		tv_maythicong_cost.setPaintFlags(tv_maythicong_cost.getPaintFlags() |   Paint.UNDERLINE_TEXT_FLAG);
		imgbtn_edit_maythicong_cost.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showNhapChiPhiDialog(FROM_TV_CHIPHI_MAY_THI_CONG, R.string.str_hint_msg_input_cost_may_thicong, R.string.str_nhapChiPhiMayThiCong);
			}
		});
		
//		tv_vanchuyen_cost = (TextView)v.findViewById(R.id.tv_vanchuyen_cost);		
//		tv_vanchuyen_cost.setPaintFlags(tv_vanchuyen_cost.getPaintFlags() |   Paint.UNDERLINE_TEXT_FLAG);
		imgbtn_edit_vanchuyen_cost.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showNhapChiPhiDialog(FROM_TV_CHIPHI_VC, R.string.str_hint_msg_input_cost_vanchuyen, R.string.str_nhapChiPhiVanChuyen);
			}
		});		
		
		return v;
	}
	
	
	
	
	private void setVisibility()
	{
		String dvdt = "";
		
		if(MainTabFragmentActivity.getInstance() != null && MainTabFragmentActivity.getInstance().getLocalChietTinhMain() != null)
			dvdt = MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_DonViDauTu();
			
		if(dvdt.equalsIgnoreCase("0")){		
		
			ttk.setVisibility(View.GONE);			
			tl.setVisibility(View.GONE);
			k.setVisibility(View.GONE);
			gtt.setVisibility(View.GONE);
			vat.setVisibility(View.GONE);		
			
		}
		else if(dvdt.equalsIgnoreCase("1") ||dvdt.equalsIgnoreCase("3")){
			ttk.setVisibility(View.GONE);			
			tl.setVisibility(View.GONE);
			k.setVisibility(View.GONE);
			gtt.setVisibility(View.GONE);
			vat.setVisibility(View.VISIBLE);		
			
		}
		else if(dvdt.equalsIgnoreCase("2")){
			
			ttk.setVisibility(View.VISIBLE);			
			tl.setVisibility(View.VISIBLE);
			k.setVisibility(View.VISIBLE);
			gtt.setVisibility(View.VISIBLE);
			vat.setVisibility(View.VISIBLE);			
		}			
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		res = getResources();
		
		listener = new UpdateListener();
		
		updateDisplayInfoGlobal();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (!UpdateListenerIsRegistered && listener != null) {
			IntentFilter intentFilter = new IntentFilter();
			intentFilter.addAction("com.application.chiettinhdienluc.ui.RefreshUserInfo");
			intentFilter.addAction("com.application.chiettinhdienluc.ui.SaveResultInfo");
			//dzung 30 10
			intentFilter.addAction("com.application.chiettinhdienluc.ui.maDonViDauTu_change");
			intentFilter.addAction("com.application.chiettinhdienluc.ui.UpdateLocalChietTinhModelResultInfo");
			intentFilter.addAction("com.application.chiettinhdienluc.ui.result_change");			
			
			getActivity().registerReceiver(listener, intentFilter);
			UpdateListenerIsRegistered = true;
		}
//		updateDisplayInfoGlobal();
	}
	
	
	
	@Override
	public void onPause() {
		try {
			if (UpdateListenerIsRegistered && listener != null) {
				getActivity().unregisterReceiver(listener);
				UpdateListenerIsRegistered = false;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		// TODO Auto-generated method stub
		
		super.onPause();
	}

	private void updateDisplayInfoGlobal()
	{
		if(MainTabFragmentActivity.getiMode() == MainTabFragmentActivity.MODE_EDITABLE)
		{
			updateView();
		}	
		else if(MainTabFragmentActivity.getiMode() == MainTabFragmentActivity.MODE_NEW_INPUT)
		{
//			if(ResultInfoFragment.needRefreshEmptyValueResultInfo)
			{	
				updateView();

				ResultInfoFragment.needRefreshEmptyValueResultInfo = false;
			}
		}	
	}
	
	protected class UpdateListener extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				if(intent != null && !TextUtils.isEmpty(intent.getAction()))
				{
					String action = intent.getAction();
					boolean updateDataForResultTab = false;
					
					if(!TextUtils.isEmpty(action))
					{
						if(action.equals("com.application.chiettinhdienluc.ui.RefreshUserInfo"))
						{ 
							edt_cost_vatlieu.setText("");
							edt_cost_vatlieu1.setText("");
							edt_vanchuyen_cost.setText("");
							edt_nhancong_cost.setText("");
							edt_maythicong_cost.setText("");
							edt_tructiep_khac_cost.setText("");
							edt_tructiep_cost.setText("");
							edt_general_cost.setText("");
							edt_tl_cost.setText("");
							edt_k_cost.setText("");
							edt_gtt_cost.setText("");
							edt_vat_cost.setText("");
							edt_total_cost.setText("");
						}
						else if(action.equals("com.application.chiettinhdienluc.ui.SaveResultInfo"))	
						{
							GlobalData.globalChietTinh.set_ChiPhiVL(getDoubleValueFromEdt(edt_cost_vatlieu));
							GlobalData.globalChietTinh.set_ChiPhiVT(getDoubleValueFromEdt(edt_cost_vatlieu1));
							GlobalData.globalChietTinh.set_ChiPhiVC(getDoubleValueFromTextView(edt_vanchuyen_cost));
							GlobalData.globalChietTinh.set_ChiPhiNC(getDoubleValueFromEdt(edt_nhancong_cost));
							GlobalData.globalChietTinh.set_ChiPhiMM(getDoubleValueFromTextView(edt_maythicong_cost));
							GlobalData.globalChietTinh.set_ChiPhiTTK(getDoubleValueFromEdt(edt_tructiep_khac_cost));
							GlobalData.globalChietTinh.set_ChiPhiTT(getDoubleValueFromEdt(edt_tructiep_cost));
							GlobalData.globalChietTinh.set_ChiPhiC(getDoubleValueFromEdt(edt_general_cost));
							GlobalData.globalChietTinh.set_ChiPhiTL(getDoubleValueFromEdt(edt_tl_cost));
							GlobalData.globalChietTinh.set_ChiPhiK(getDoubleValueFromEdt(edt_k_cost));
							GlobalData.globalChietTinh.set_TienGTT(getDoubleValueFromEdt(edt_gtt_cost));
							GlobalData.globalChietTinh.set_TienGTGT(getDoubleValueFromEdt(edt_vat_cost));
							GlobalData.globalChietTinh.set_SoTien(getDoubleValueFromEdt(edt_total_cost));
							
							if(MainTabFragmentActivity.getInstance() != null && MainTabFragmentActivity.getInstance().getLocalChietTinhMain() != null)
								GlobalData.globalChietTinh.set_LamTron(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_LamTron());
							
							if(MainApplication.getAppContext() != null)
							{
								Intent broadcastIntent = new Intent();
								broadcastIntent.setAction("com.application.chiettinhdienluc.ui.SaveUserInfo");
								MainApplication.getAppContext().sendBroadcast(broadcastIntent);
							}
						}
						else if (action.equalsIgnoreCase("com.application.chiettinhdienluc.ui.UpdateLocalChietTinhModelResultInfo"))
						{
							updateDataForResultTab = intent != null ? intent.getBooleanExtra("UpdateDataForResultTab", false) : false;
							
							MainTabFragmentActivity.getInstance().getLocalChietTinhMain().set_ChiPhiVL(getDoubleValueFromEdt(edt_cost_vatlieu));
							MainTabFragmentActivity.getInstance().getLocalChietTinhMain().set_ChiPhiVT(getDoubleValueFromEdt(edt_cost_vatlieu1));
							MainTabFragmentActivity.getInstance().getLocalChietTinhMain().set_ChiPhiVC(getDoubleValueFromTextView(edt_vanchuyen_cost));
							MainTabFragmentActivity.getInstance().getLocalChietTinhMain().set_ChiPhiNC(getDoubleValueFromEdt(edt_nhancong_cost));
							MainTabFragmentActivity.getInstance().getLocalChietTinhMain().set_ChiPhiMM(getDoubleValueFromTextView(edt_maythicong_cost));
							MainTabFragmentActivity.getInstance().getLocalChietTinhMain().set_ChiPhiTTK(getDoubleValueFromEdt(edt_tructiep_khac_cost));
							MainTabFragmentActivity.getInstance().getLocalChietTinhMain().set_ChiPhiTT(getDoubleValueFromEdt(edt_tructiep_cost));
							MainTabFragmentActivity.getInstance().getLocalChietTinhMain().set_ChiPhiC(getDoubleValueFromEdt(edt_general_cost));
							MainTabFragmentActivity.getInstance().getLocalChietTinhMain().set_ChiPhiTL(getDoubleValueFromEdt(edt_tl_cost));
							MainTabFragmentActivity.getInstance().getLocalChietTinhMain().set_ChiPhiK(getDoubleValueFromEdt(edt_k_cost));
							MainTabFragmentActivity.getInstance().getLocalChietTinhMain().set_TienGTT(getDoubleValueFromEdt(edt_gtt_cost));
							MainTabFragmentActivity.getInstance().getLocalChietTinhMain().set_TienGTGT(getDoubleValueFromEdt(edt_vat_cost));
							MainTabFragmentActivity.getInstance().getLocalChietTinhMain().set_SoTien(getDoubleValueFromEdt(edt_total_cost));
							
							if(MainApplication.getAppContext() != null)
							{
								Intent broadcastIntent = new Intent();
								broadcastIntent.setAction("com.application.chiettinhdienluc.ui.UpdateLocalChietTinhModelUserInfo");
								if(updateDataForResultTab) broadcastIntent.putExtra("UpdateDataForResultTab", true);
								MainApplication.getAppContext().sendBroadcast(broadcastIntent);
							}
						}
//						else if (action.equalsIgnoreCase("com.application.chiettinhdienluc.ui.maDonViDauTu_change"))
//						{
//							if(MainTabFragmentActivity.getInstance() != null)
//							{
//								CalculationUtils.calculate(MainTabFragmentActivity.getInstance().getLocalChietTinhMain(), (long)GlobalData.ChiPhiVatTu, (long)GlobalData.ChiPhiNhanCong);
//	 						    updateView();
//								setVisibility();
//							}
//						}
						else if (action.equalsIgnoreCase("com.application.chiettinhdienluc.ui.result_change"))
						{
//							updateView();
							if(MainTabFragmentActivity.getInstance() != null && MainTabFragmentActivity.getInstance().getLocalChietTinhMain() != null)
							{
								edt_cost_vatlieu.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiVL()));
								edt_cost_vatlieu1.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiVT()));
								edt_vanchuyen_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiVC()));
								edt_nhancong_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiNC()));
								edt_maythicong_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiMM()));
								edt_tructiep_khac_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiTTK()));
								edt_tructiep_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiTT()));
								edt_general_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiC()));
								edt_tl_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiTL()));
								edt_k_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiK()));
								edt_gtt_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_TienGTT()));
								edt_vat_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_TienGTGT()));
								edt_total_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_SoTien()));
								
								setVisibility();
							}
						}					
					}	
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	private double getDoubleValueFromEdt(EditText widget)
	{
		double result = 0;
		try {
			result = Utils.removeCommaFromMoney(widget.getText().toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}

	private double getDoubleValueFromTextView(TextView widget)
	{
		double result = 0;
		try {
			result = Utils.removeCommaFromMoney(widget.getText().toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}
	
	private void updateView() {
		if(MainTabFragmentActivity.getInstance() != null && MainTabFragmentActivity.getInstance().getLocalChietTinhMain() != null)
		{
			edt_cost_vatlieu.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiVL()));
			edt_cost_vatlieu1.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiVT()));
			edt_vanchuyen_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiVC()));
			edt_nhancong_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiNC()));
			edt_maythicong_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiMM()));
			edt_tructiep_khac_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiTTK()));
			edt_tructiep_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiTT()));
			edt_general_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiC()));
			edt_tl_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiTL()));
			edt_k_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_ChiPhiK()));
			edt_gtt_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_TienGTT()));
			edt_vat_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_TienGTGT()));
			edt_total_cost.setText(Utils.formatDoubleNumber(MainTabFragmentActivity.getInstance().getLocalChietTinhMain().get_SoTien()));
		}
		else
		{
			edt_cost_vatlieu.setText(Utils.formatDoubleNumber(GlobalData.globalChietTinh.get_ChiPhiVL()));
			edt_cost_vatlieu1.setText(Utils.formatDoubleNumber(GlobalData.globalChietTinh.get_ChiPhiVT()));
			edt_vanchuyen_cost.setText(Utils.formatDoubleNumber(GlobalData.globalChietTinh.get_ChiPhiVC()));
			edt_nhancong_cost.setText(Utils.formatDoubleNumber(GlobalData.globalChietTinh.get_ChiPhiNC()));
			edt_maythicong_cost.setText(Utils.formatDoubleNumber(GlobalData.globalChietTinh.get_ChiPhiMM()));
			edt_tructiep_khac_cost.setText(Utils.formatDoubleNumber(GlobalData.globalChietTinh.get_ChiPhiTTK()));
			edt_tructiep_cost.setText(Utils.formatDoubleNumber(GlobalData.globalChietTinh.get_ChiPhiTT()));
			edt_general_cost.setText(Utils.formatDoubleNumber(GlobalData.globalChietTinh.get_ChiPhiC()));
			edt_tl_cost.setText(Utils.formatDoubleNumber(GlobalData.globalChietTinh.get_ChiPhiTL()));
			edt_k_cost.setText(Utils.formatDoubleNumber(GlobalData.globalChietTinh.get_ChiPhiK()));
			edt_gtt_cost.setText(Utils.formatDoubleNumber(GlobalData.globalChietTinh.get_TienGTT()));
			edt_vat_cost.setText(Utils.formatDoubleNumber(GlobalData.globalChietTinh.get_TienGTGT()));
			edt_total_cost.setText(Utils.formatDoubleNumber(GlobalData.globalChietTinh.get_SoTien()));
		}
	}
	
	private boolean isShowDialog = false;
	private Dialog dialog;
	private EditText edNhap;
	private Button btnSave;
	private Button btnCancel;
	private TextView tvMessage;
	private TextView tvName;

	private void showNhapChiPhiDialog(final int fromSource, int resourceIdMsg, int resourceIdTitle) {
		if (!isShowDialog) {
			isShowDialog = true;
			dialog = new Dialog(getActivity());
			dialog.getWindow();
			dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			dialog.setContentView(R.layout.dialog_nhapsoluong);
			dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

			this.btnSave = (Button) dialog.findViewById(R.id.btnSave);
			this.btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
			this.edNhap = (EditText) dialog.findViewById(R.id.edNhap);
			this.tvMessage = (TextView)dialog.findViewById(R.id.tvMessage);
			this.tvName = (TextView)dialog.findViewById(R.id.tvName);
			
			tvMessage.setText(res.getString(resourceIdMsg));
			tvName.setText(res.getString(resourceIdTitle));
		
			btnCancel.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(isShowDialog && dialog != null)
						dialog.dismiss();
				}
			});
			btnSave.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (checkConstraint()){
						if(fromSource == FROM_TV_CHIPHI_VC)
						{
							edt_vanchuyen_cost.setText(edNhap.getText().toString());
							String vanchuyen_cost = edt_vanchuyen_cost.getText().toString().trim();
							double vanchuyenCost;
							if (!TextUtils.isEmpty(vanchuyen_cost)) {
								vanchuyenCost= Double.parseDouble(vanchuyen_cost) ;
							} else {
								vanchuyenCost = 0;
							}
//							GlobalData.globalChietTinh.set_ChiPhiVC((long)vanchuyenCost);
						}	
						else if(fromSource == FROM_TV_CHIPHI_MAY_THI_CONG)
						{
							edt_maythicong_cost.setText(edNhap.getText().toString());
							String maythicong_cost = edt_maythicong_cost.getText().toString().trim();
							double maythicongCost;
							if (!TextUtils.isEmpty(maythicong_cost)) {
								maythicongCost= Double.parseDouble(maythicong_cost) ;
							} else {
								maythicongCost = 0;
							}
//							GlobalData.globalChietTinh.set_ChiPhiMM((long)maythicongCost);
						}	
						
//						CalculationUtils.calculate(GlobalData.globalChietTinh, (long)GlobalData.ChiPhiVatTu, (long)GlobalData.ChiPhiNhanCong);
//						updateView();
						
						Intent broadcastIntent = new Intent();
		                broadcastIntent.setAction("com.application.chiettinhdienluc.ui.UpdateLocalChietTinhModelResultInfo");
		                broadcastIntent.putExtra("UpdateDataForResultTab", true);
		                MainApplication.getAppContext().sendBroadcast(broadcastIntent);
					}
					
					dialog.dismiss();
					isShowDialog = false;
				}
			});

			dialog.setOnDismissListener(new OnDismissListener() {
				@Override
				public void onDismiss(DialogInterface dialog) {
					// TODO Auto-generated method stub
					isShowDialog = false;
				}
			});

			dialog.show();
		}   
	}

	
//	public void updateDataAndRecalculate(View arg0) {
//		// TODO Auto-generated method stub
//		
//		String text = edt_vanchuyen_cost.getText().toString().trim();
//		long vanchuyenCost;
//		if (!TextUtils.isEmpty(text)) {
//			vanchuyenCost= Utils.removeCommaFromMoney(text) ;
//		} else {
//			vanchuyenCost = 0;
//		}
//		
//		GlobalData.globalChietTinh.set_ChiPhiVC((long)vanchuyenCost);
//		CalculationUtils.calculate(GlobalData.globalChietTinh, (long)GlobalData.ChiPhiVatTu, (long)GlobalData.ChiPhiNhanCong);
//		//updateView();	
//	}

        public boolean checkConstraint() {

		if (TextUtils.isEmpty(edNhap.getText().toString())) {
			edNhap.setError("Error");
			return false;
		}

		return true;
	}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
		}

//	@Override
//	public void onClick(View v) {
//		if (v == btnCancel) {
//			dialog.dismiss();
//		}
//		
//		if (v == btnSave) {
//			if (checkConstraint()) {
//				edt_vanchuyen_cost.setText(edNhap.getText().toString());
//				dialog.dismiss();
//			}
//		}
//	}
	
}